import {onMounted, ref} from "vue";
import {getCategoryAPI} from "@/apis/category.js";
import {onBeforeRouteUpdate, useRoute} from "vue-router";
/**
 * 封装category分类数据业务代码
 */
export function useCategory(){
    const route = useRoute()
    const categoryData = ref([])
    const getCategory = async (id = route.params.id) => {
        const res = await getCategoryAPI(id)
        categoryData.value = res.result
    }
    onMounted(() => {
        getCategory()
    })
    // 路由参数变化的时候，可以把分类数据接口重新发送
    onBeforeRouteUpdate((to)=> {
        getCategory(to.params.id)
    })
    return {
        categoryData
    }
}
